package com.hippo.core.ui.compose.control

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hippo.core.R
import com.hippo.core.ui.components.BottomSheetDialog
import com.hippo.core.ui.components.CheckText
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.theme.HippoTheme

@Composable
fun RepeatDialog(loops:String,onDismissRequest: () -> Unit, onSure: (String,String) -> Unit) {


    BottomSheetDialog(
        title = stringResource(R.string.repeat),
        onDismissRequest = onDismissRequest,
    ) {
        DialogContent(loops,onSure,onDismissRequest)
    }

}


@Composable
private fun DialogContent(loop:String,onSure: (String,String) -> Unit,onDismissRequest: () -> Unit) {

    var loops by remember { mutableStateOf(loop) }

    val loopsArray = loops.toCharArray()

    Column {

        val week = stringArrayResource(R.array.week)

        Text(
            text = stringResource(R.string.repeat_1),
            style = MaterialTheme.typography.bodyMedium
        )

        Spacer(modifier = Modifier.height(16.dp))

        Column(
            modifier = Modifier
                .background(
                    color = Color.White,
                    shape = MaterialTheme.shapes.medium
                )
        ) {


            CheckText(stringResource(R.string.Monday), loopsArray[0] == '1') {
                if (it) {
                    loopsArray[0] = '1'
                } else {
                    loopsArray[0] = '0'
                }
                loops = String(loopsArray)
            }

            HorizontalDivider(thickness = 0.5.dp)

            CheckText(stringResource(R.string.Tuesday), loopsArray[1] == '1') {
                if (it) {
                    loopsArray[1] = '1'
                } else {
                    loopsArray[1] = '0'
                }
                loops = String(loopsArray)
            }
            HorizontalDivider(thickness = 0.5.dp)

            CheckText(stringResource(R.string.Wednesday), loopsArray[2] == '1') {
                if (it) {
                    loopsArray[2] = '1'
                } else {
                    loopsArray[2] = '0'
                }
                loops = String(loopsArray)
            }
            HorizontalDivider(thickness = 0.5.dp)

            CheckText(stringResource(R.string.Thursday), loopsArray[3] == '1') {
                if (it) {
                    loopsArray[3] = '1'
                } else {
                    loopsArray[3] = '0'
                }
                loops = String(loopsArray)
            }
            HorizontalDivider(thickness = 0.5.dp)

            CheckText(stringResource(R.string.Friday), loopsArray[4] == '1') {
                if (it) {
                    loopsArray[4] = '1'
                } else {
                    loopsArray[4] = '0'
                }
                loops = String(loopsArray)
            }
            HorizontalDivider(thickness = 0.5.dp)

            CheckText(stringResource(R.string.Saturday), loopsArray[5] == '1') {
                if (it) {
                    loopsArray[5] = '1'
                } else {
                    loopsArray[5] = '0'
                }
                loops = String(loopsArray)
            }
            HorizontalDivider(thickness = 0.5.dp)

            CheckText(stringResource(R.string.Sunday), loopsArray[6] == '1') {
                if (it) {
                    loopsArray[6] = '1'
                } else {
                    loopsArray[6] = '0'
                }
                loops = String(loopsArray)
            }

        }



        Spacer(modifier = Modifier.height(16.dp))

        ThemeButton(text = stringResource(R.string.save)) {

            val days = StringBuilder()

            loopsArray.forEachIndexed { index ,item ->
                if(item == '1'){
                    days.append(week[index]).append(" ")
                }
            }

            onDismissRequest()

            onSure(loops,days.toString())
        }

        Spacer(modifier = Modifier.height(16.dp))

    }
}


@Preview(showBackground = true)
@Composable
private fun RepeatScreenPreview() {
    HippoTheme {
        DialogContent(
           loop = "0001100",
            onSure = {_,_ ->},
            onDismissRequest = {}
           )
    }
}

